package subject;


import event.MessageEvent;
import observer.ComponentListener;

import java.util.LinkedList;
import java.util.List;

/**
 * 这是一个会主动推送的总消息通道，当有信息传入的时候
 * 会通知所有订阅成员
 */
public class ZhhMessageBus {
    
    private final static int MAX_SIZE = 100;

    private final List<ComponentListener> listeners = new LinkedList<>();

    public boolean addListener(ComponentListener listener) {
        if (listeners.size() >= MAX_SIZE) {
            return false;
        }
        return listeners.add(listener);
    }

    public void remove(ComponentListener listener) {
        this.listeners.remove(listener);
    }

    public void sendMessage(MessageEvent event) {
        notifyObservers(event);
    }

    private void notifyObservers(MessageEvent event) {
        for (ComponentListener observer : listeners) {
            observer.handle(event);
        }
    }


    
}
